#!/bin/bash
# echo the call so we can see what's going on
echo "$0 $1 $2 $3 $4 $5 $6"

# Check syntax
if [ "$1" = "" ]; then
	echo "$0 called without file name for histogram!"
	exit 0
fi

if [ -e $1 ]; then
	echo "Your data file $1 was found!"
else
	echo "Your data file $1 was NOT found!"
	read -p "You should not continue here! Please hit Ctrl-C until you are out of the script!"
	exit
fi

if [ "$2" = "" ]; then
	echo "$0 called without instrument parameter file for histogram!"
	exit 0
fi

if [ -e $2 ]; then
	echo "Your instrument parameter file $2 was found!"
else
	echo "Your instrument parameter file $2 was NOT found!"
	read -p "You should not continue here! Please hit Ctrl-C until you are out of the script!"
	exit
fi

if [ "$3" = "" ]; then
	echo "$0 called without bank number for histogram!"
	exit 0
fi

if [ "$4" = "" ]; then
	echo "$0 called without minimum d-spacing for histogram!"
	exit 0
fi

if [ "$5" = "" ]; then
	echo "$0 called without maximum d-spacing for histogram, assuming no upper limit!"
fi

# Add the histogram
echo "k p h i" > temp.txt
echo $1 >> temp.txt
echo "y" >> temp.txt
grep "Instrument parameter file" $1 > out.txt
if [ "$?" = "1" ]; then
	echo "IPARM file is not stored in the data file!"
else
	echo "There is an IPARM file given in the data file, will overwrite with $2..."
	echo "n" >> temp.txt
fi
echo $2 >> temp.txt
echo $3 >> temp.txt
echo "n" >> temp.txt
# give out a few zeros - if we use a bank multiple times it will get us out of the warning for adding banks with same parameters
# if it is the first time the bank is used, it won't do any harm...
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "0" >> temp.txt
echo "d" >> temp.txt
if [ "$5" != "" ]; then
	# We have a maximum d-spacing, let's convert it to the native x-axis value (TOF or 2theta)
	echo $5 >> temp.txt
fi
echo $4 >> temp.txt
echo "/" >> temp.txt
echo "x" >> temp.txt
echo "0" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt
echo "x" >> temp.txt

expedt `cat GSAS_EXP` < temp.txt > out.txt
# read -p "wait after first insert..."

# did we get the warning about re-using the instrument data?
grep "WARNING - instrument data for this bank also used for histogram" out.txt > temp2.txt
if [ "$?" = "0" ]; then
  echo "Re-using the instrument parameters - need to re-insert"
  sed '7i\y' temp.txt > temp2.txt
#   read -p "wait before second insert..."
  expedt `cat GSAS_EXP` < temp2.txt > out.txt
#   read -p "wait after second insert..."
fi

# Which histogram in the EXP file did we add?
grep "Histogram no." out.txt | head -c18 | tail -c2 > hist.txt

# Test the EXP file
grep "$1" `cat GSAS_EXP`.EXP > temp2.txt
#echo "grep returned $?"
if [ "$?" = "1" ]; then
	echo "Data file name is NOT in the EXP file"
	read -p "Maybe you want to debug?"
	exit 0
fi

grep "$2" `cat GSAS_EXP`.EXP > temp.txt
#echo "grep returned $?"
if [ "$?" = "1" ]; then
	echo "Instrument parameter file name is NOT in the EXP file"
	read -p "Maybe you want to debug?"
	exit 0
fi

if [ $3 -lt 10 ]; then
	grep "BANK     $3" `cat GSAS_EXP`.EXP > temp.txt
else 
	grep "BANK    $3" `cat GSAS_EXP`.EXP > temp.txt
fi

#echo "grep returned $?"
if [ "$?" = "1" ]; then
	echo "Entry with this bank is NOT in the EXP file"
	exit 0
fi

# check if we have bc installed - it's not standard on cygwin
bc_path=`which bc`
if [ "$bc_path" = "" ]; then
	echo "You don't have the calculator bc installed!"
	echo "Please install for this function.!"
	read -p "You should not continue here! Please hit Ctrl-C until you are out of the script!"
	exit
else
	echo "Calculator bc found."
fi

if [ "$5" != "" ]; then
	# We have a maximum d-spacing, let's enter it
	if [ `echo "$5 < 10" | bc ` ]; then
		grep "d-minimum" out.txt | grep " of  $5" > maxtof.txt
	else
		grep "d-minimum" out.txt | grep " of $5" > maxtof.txt
	fi
	tail -c 15 maxtof.txt > maxtof2.txt
	head -c 8 maxtof2.txt > maxtof.txt
	rm maxtof2.txt

	echo "k p h" > temp.txt
	echo "e `cat hist.txt`" >> temp.txt
	echo "e" >> temp.txt
	# now we need to know if we are dealing with CW or TOF to find out which region to modify...
	grep HTYPE $2 | head -c 17 > htype.txt
	if [ "`tail -c 1 htype.txt`" == "C" ]; then
	    echo "We have a constant wavelength instrument, need to modify excluded region 1 for maximum d-spacing"
	    echo "c 1" >> temp.txt
	    echo "0 `cat maxtof.txt` " >> temp.txt
	else
	    if [ "`tail -c 1 htype.txt`" == "T" ]; then
		echo "We have a time-of-flight instrument, need to modify excluded region 2 for maximum d-spacing"
		echo "c 2" >> temp.txt
		echo "`cat maxtof.txt` 1000" >> temp.txt
	    else
		echo "We have a unknown type of instrument - need debugging!"
		read -p "Break here with Ctrl-c and debug!"
	    fi
	fi
	echo "x" >> temp.txt
	echo "x" >> temp.txt
	echo "x" >> temp.txt
	echo "x" >> temp.txt
	echo "x" >> temp.txt
	echo "x" >> temp.txt
	expedt `cat GSAS_EXP` < temp.txt > out.txt
# 	read -p "wait after entering d-spacing..."
	rm htype.txt
fi

if [ -e temp2 ]; then
  rm temp2.txt
fi

echo "Added histogram from \verb=$1= (bank $3, min. d-spacing $4A, max. d-spacing $5A) with instrument parameter file \verb=$2= as histogram `cat hist.txt` to EXP file.\newline" >> `cat GSAS_EXP`.tex

